/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Logica.Garantia;
import Logica.Modelo;
import Logica.Turno;
import Logica.Vehiculo;
import Persistencia.exceptions.PreexistingEntityException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JDesktopPane;

/**
 *
 * @author Alberto
 */
public class frmAltaVehiculo extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual;
    JDesktopPane miEscritorio;
    DefaultComboBoxModel modeloModelos = new DefaultComboBoxModel();
    DefaultListModel modeloVehiculos = new DefaultListModel();

    /**
     * Creates new form AltaVehiculo
     */
    public frmAltaVehiculo(ControladoraVisual miVisual, JDesktopPane miEscritorio) {
        initComponents();
        this.miVisual = miVisual;
        this.miEscritorio = miEscritorio;
        this.cmbModelos.setModel(modeloModelos);
        this.cargarModelos();
    }

    public frmAltaVehiculo(ControladoraVisual miVisual, JDesktopPane miEscritorio, DefaultListModel modeloVehiculos) {
        initComponents();
        this.miVisual = miVisual;
        this.miEscritorio = miEscritorio;
        this.cmbModelos.setModel(modeloModelos);
        this.cargarModelos();
        this.modeloVehiculos = modeloVehiculos;
    }

    public void cargarModelos() {
        List<Modelo> modelos = miVisual.dameModelos();
        for (Modelo unModelo : modelos) {
            this.cmbModelos.addItem(unModelo);
        }
    }

    public Garantia cargarGarantia() {
        Garantia suGarantia = null;
        Calendar hastaFecha = null;

        SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yy");
        String fec = cmbHastaFecha.getText();
        try {
            Date fecha1 = formateador.parse(fec);
            hastaFecha = Calendar.getInstance();
            hastaFecha.setTime(fecha1);
        } catch (ParseException ex) {
            Logger.getLogger(frmAltaVehiculo.class.getName()).log(Level.SEVERE, null, ex);
        }
        int hastaKm = Integer.valueOf(txtHastaKm.getText());
        int nroGarantia = Integer.valueOf(txtNroGarantia.getText());
        try {
            suGarantia = miVisual.altaGarantia(nroGarantia, hastaFecha, hastaKm, null);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaVehiculo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaVehiculo.class.getName()).log(Level.SEVERE, null, ex);
        }

        return suGarantia;
    }

    public Calendar cargarFecha() {
        Calendar fechaCompra = null;

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
        String fechita = cmbFechas.getText();
        try {
            Date fecha = formatter.parse(fechita);
            fechaCompra = Calendar.getInstance();
            fechaCompra.setTime(fecha);
        } catch (ParseException ex) {
            Logger.getLogger(frmAltaVehiculo.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fechaCompra;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtNroChasis = new javax.swing.JTextField();
        txtDominio = new javax.swing.JTextField();
        txtNroMotor = new javax.swing.JTextField();
        txtKmRec = new javax.swing.JTextField();
        txtEquipamiento = new javax.swing.JTextField();
        txtAnio = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        btnAltaVehiculo = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        cmbModelos = new javax.swing.JComboBox();
        btnAltaModelo = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cmbHastaFecha = new datechooser.beans.DateChooserCombo();
        jLabel8 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtHastaKm = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtNroGarantia = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        cmbFechas = new datechooser.beans.DateChooserCombo();

        setTitle("Alta Vehiculo");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setText("Nro de Chasis");

        jLabel2.setText("Dominio");

        jLabel3.setText("Nro de Motor");

        jLabel4.setText("Km recorridos");

        jLabel5.setText("Equipamiento");

        jLabel6.setText("Fecha Compra");

        jLabel7.setText("Año");

        txtNroChasis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNroChasisActionPerformed(evt);
            }
        });

        txtKmRec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtKmRecActionPerformed(evt);
            }
        });

        btnAltaVehiculo.setText("Alta Vehiculo");
        btnAltaVehiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaVehiculoActionPerformed(evt);
            }
        });

        jLabel9.setText("Modelo");

        cmbModelos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbModelos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbModelosActionPerformed(evt);
            }
        });

        btnAltaModelo.setText("Nuevo Modelo");
        btnAltaModelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaModeloActionPerformed(evt);
            }
        });

        jLabel10.setText("DATOS DE GARANTIA");

        jLabel8.setText("Hasta Fecha");

        jLabel12.setText("Hasta Km");

        jLabel13.setText("Nro de Garantía");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAltaVehiculo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSalir))
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbFechas, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                    .addComponent(txtAnio, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtNroMotor, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtKmRec, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtEquipamiento, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel2))
                                    .addGap(15, 15, 15)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtDominio, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                                        .addComponent(txtNroChasis)))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(btnAltaModelo)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel12)
                                            .addComponent(jLabel13))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(cmbHastaFecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(txtHastaKm)
                                            .addComponent(txtNroGarantia)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(cmbModelos, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jSeparator2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel11)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNroChasis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(txtDominio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11)
                                .addComponent(jLabel8))
                            .addComponent(cmbHastaFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtNroMotor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12)
                            .addComponent(txtHastaKm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtKmRec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13)
                            .addComponent(txtNroGarantia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtEquipamiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jLabel9)
                        .addComponent(cmbModelos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbFechas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAltaModelo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAltaVehiculo))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void txtNroChasisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNroChasisActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNroChasisActionPerformed

    private void txtKmRecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtKmRecActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtKmRecActionPerformed

    private void cmbModelosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbModelosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbModelosActionPerformed

    private void btnAltaVehiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaVehiculoActionPerformed

        // Garantía //
        Garantia suGarantia = this.cargarGarantia();


        // Vehiculo //
        String nroChasis = txtNroChasis.getText();
        String dominio = txtDominio.getText();
        String nroMotor = txtNroMotor.getText();
        int kmRec = Integer.valueOf(txtKmRec.getText());
        String equipamiento = txtEquipamiento.getText();
        Calendar fechaCompra = this.cargarFecha();
        int anio = Integer.valueOf(txtAnio.getText());
        Modelo modelo = (Modelo) cmbModelos.getSelectedItem();
        Turno unTurno = null;
        try {
            Vehiculo unVehiculo = miVisual.altaVehiculo(dominio, nroChasis, nroMotor, anio, kmRec, equipamiento,
                    fechaCompra, modelo, suGarantia, unTurno, null);
            this.actualizarVentana(unVehiculo);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaVehiculo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaVehiculo.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_btnAltaVehiculoActionPerformed

    private void btnAltaModeloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaModeloActionPerformed
        frmAltaModelo unModelo = new frmAltaModelo(miVisual, miEscritorio, modeloModelos);
        miEscritorio.add(unModelo);
        unModelo.setMaximizable(true);
        unModelo.setClosable(true);
        unModelo.setVisible(true);
    }//GEN-LAST:event_btnAltaModeloActionPerformed
    public void actualizarVentana(Vehiculo unVehiculo) {
        this.modeloVehiculos.addElement(unVehiculo);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAltaModelo;
    private javax.swing.JButton btnAltaVehiculo;
    private javax.swing.JButton btnSalir;
    private datechooser.beans.DateChooserCombo cmbFechas;
    private datechooser.beans.DateChooserCombo cmbHastaFecha;
    private javax.swing.JComboBox cmbModelos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField txtAnio;
    private javax.swing.JTextField txtDominio;
    private javax.swing.JTextField txtEquipamiento;
    private javax.swing.JTextField txtHastaKm;
    private javax.swing.JTextField txtKmRec;
    private javax.swing.JTextField txtNroChasis;
    private javax.swing.JTextField txtNroGarantia;
    private javax.swing.JTextField txtNroMotor;
    // End of variables declaration//GEN-END:variables
}
